package com.atguigu.dga230315.governance.assess;

import com.atguigu.dga230315.governance.bean.AssessParam;
import com.atguigu.dga230315.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230315.governance.bean.GovernanceMetric;
import com.atguigu.dga230315.meta.bean.TableMetaInfo;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class Assessor {

    protected abstract void assess(GovernanceAssessDetail assessDetail,AssessParam assessParam) throws ParseException;

    public final GovernanceAssessDetail mainAssess(AssessParam assessParam) {
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        //设置考评结果的默认值
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setMetricId(governanceMetric.getId()+"");
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);
        try {
            //具体的考评方法
            assess(governanceAssessDetail,assessParam);
        }catch (Exception e){
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,Math.min(stringWriter.toString().length(),2000)));
        }
        governanceAssessDetail.setCreateTime(new Date());
        return governanceAssessDetail;
    }
}
